﻿using System;
using System.Collections.Generic;
using System.IO;

namespace MediaStreamer.Containers
{
    public class PageTemplate
    {
        public PageTemplateSettings Settings { get; set; }
        public string BaseDirectory { get; set; }
        public string TemplateDirectory { get; set; }
        public string Raw { get; set; }

        public PageTemplate(string baseDir, string pageTemplate)
        {
            BaseDirectory = baseDir;
            TemplateDirectory = pageTemplate;
            Load();
        }

        public void Load()
        {
            // Make sure basic template exist
            if (!File.Exists(TemplateDirectory + "index.html"))
                throw new ArgumentException("templateDir is not a valid PageTemplate location.");
            // Make sure template settings exist
            if (!File.Exists(TemplateDirectory + "settings.txt"))
                throw new ArgumentException("templateDir is not a valid PageTemplate location.");

            Raw = File.ReadAllText(TemplateDirectory + "index.html");
            ParseSettings();
        }

        protected void ParseSettings()
        {
            var tempSettings = new PageTemplateSettings();

            string[] lines = File.ReadAllLines(TemplateDirectory + "settings.txt");

            foreach (var line in lines)
            {
                var pair = line.Split(':');
                if (pair.Length != 2)
                    continue;

                var key = pair[0].ToLower();
                var value = pair[1];

                switch (key)
                {
                    case "controls":
                        tempSettings.Controls = value.Split(new[] {','});
                        break;
                }
            }

            Settings = tempSettings;
        }
    }

    public class PageTemplateSettings
    {
        public IEnumerable<string> Controls { get; set; }
    }
}
